package com.qianchang.microfinance.dao;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.dm.library.utils.IOUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * 版 权: 深圳市迪蒙网络科技有限公司
 * 描 述:
 * 修 改 人: zhoulai
 * 修改时间: 2017/7/20 17:34
 */


public class SQLdm {


    public void copyDataBase(Context context, String dbName) throws IOException {
        InputStream myInput = context.getAssets().open(dbName);
        File outFileName = context.getDatabasePath(dbName);
        OutputStream myOutput = null;
        try {
            if (!outFileName.exists()) {
                outFileName.getParentFile().mkdirs();
                myOutput = new FileOutputStream(outFileName);
                byte[] buffer = new byte[1024];
                int length;
                while ((length = myInput.read(buffer)) > 0) {
                    myOutput.write(buffer, 0, length);
                }
                myOutput.flush();
            }
        } finally {
            IOUtils.closeQuietly(myOutput);
            IOUtils.closeQuietly(myInput);
        }

    }

    public SQLiteDatabase openDatabase(Context context, String dbName) {
        File bdFile = context.getDatabasePath(dbName);
        if (bdFile.exists()) {
            return SQLiteDatabase.openOrCreateDatabase(bdFile, null);
        } else {
            return null;
        }
    }
}

